// pages/exam_person/exam_person.js
Page({

  /**
   * Page initial data
   */
  data: {
    studentId: "",
    name: "",
    grades: [],
    disabled: false,
    hint:""
  },

  formSubmit: function(e) {
    this.setData({
      disabled: true
    })
    wx.showLoading({
      title: '提交中',
    })


    console.log(e)
    var grades = []
    for (var i = 1; i < 8; i++) {
      grades.push(e.detail.value[i + ""])
    }

    //打分
    wx.request({
      url: getApp().globalData.my_server + "/mark/grade",
      data: {
        studentId: this.data.studentId,
        name: this.data.name,
        exam_id: getApp().globalData.exam._id,
        grades: grades
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          wx.hideLoading()
          wx.navigateBack({
            delta: 1
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '打分失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {
    var that = this
    this.setData({
      studentId: options.studentId,
      name: options.name,
      hint: options.hint
    })

    //获取当前学生
    wx.request({
      url: getApp().globalData.my_server + "/mark/get_current_student",
      data: {
        studentId: options.studentId,
        name: options.name,
        exam_id: getApp().globalData.exam._id
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          that.setData({
            grades: res.data.grade.grades
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '获取当前学生失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  }
})